<?php

namespace frontend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
/**
 * This is the model class for table "Config".
 *
 */
class Config extends ActiveRecord
{

    public $site_logo;
    public $site_title;
    public $site_phone;
    public $site_address;
    public $site_record_no;
    public $site_mail;
    public $wx_qr_code;
    public $wb_qr_code;
    public $postcode;
    public $fax_no;
    public $work_time;


    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%config}}';
    }



    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['site_logo','site_title','site_phone','site_address','site_record_no','site_mail','wx_qr_code','wb_qr_code','postcode','fax_no','work_time'], 'trim'],
            [['site_logo','site_title','site_phone','site_address','site_record_no','site_mail','wx_qr_code','wb_qr_code','postcode','fax_no','work_time'], 'required', 'message'=>'{attribute}不能为空'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'site_logo'=>'网站logo',
            'site_title'=>'网站标题',
            'site_phone'=>'联系电话',
            'site_address'=>'联系地址',
            'site_record_no'=>'网站备案号',
            'site_mail'=>'网站邮箱',
            'wx_qr_code'=>'微信二维码',
            'wb_qr_code'=>'微博二维码',
            'postcode'=>'邮政编号',
            'fax_no'=>'传真电话',
            'work_time'=>'工作时间',
        ];
    }



    public function scenarios()
    {
        return [
            'default' => [
                'site_logo',
                'site_title',
                'site_phone',
                'site_address',
                'site_record_no',
                'site_mail',
                'wx_qr_code',
                'wb_qr_code',
                'postcode',
                'fax_no',
                'work_time',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update' => [
                ]

        ];
    }



    /**
     * 获取配置参数
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getConfigInfo(){
        $list = (new Query())
            ->select('key,value')
            ->from('config')
            ->all();
        $data=[];
        if(count($list)){
            foreach($list as $k=>$v){
                $data[$v['key']] = $v['value'];
            }
        }
        return empty($data)?array():$data;

    }




    /**
     * 编辑配置
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function editConfigInfo($param){
        $t = static::getDb()->beginTransaction();
        try {
            foreach($param as $key=>$value) {
                $model = static::find()->where(['key' => $key])->one();
                $model->value = $value;
                $model->scenario='update';
                if (!$model->save()) {
                    $t->rollBack();
                    return array('error' => '操作失败');
                }
            }
            $model->refresh();
            $t->commit();
            return array('error' => '');
        }catch (Exception $e) {
            $t->rollback();
            return array('error'=>'操作失败');
        }

    }









}
